﻿using System.Collections.Generic;
using KLS.PPDS.Utilities.BaseModels;
using MvcContrib.UI.Grid;
using MvcContrib.Pagination;
using System.Collections;

namespace KLS.PPDS.Utility.BaseModels
{
    public abstract class PaginationBaseModel<T> : ViewModelBase, IEnumerable<T>
    {        

        /// <summary>
        /// Sort options
        /// </summary>
        public virtual GridSortOptions SortOptions { get; set; }

        public virtual IPagination CreatePagination(IEnumerable items)
        {
            return (IPagination)items;
        }

        public virtual IPagination Pagination { get { return (IPagination)Items; } }


        public virtual IEnumerable<T> Items { get; set; }

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            return this.Items.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.Items.GetEnumerator();
        }

        #endregion


        public PaginationBaseModel()
        {
            Items = new List<T>();            
        }

        public virtual void ApplyPagination(int? page)
        {
            ApplyPagination(page, CommonSession.PageSize);
        }

        public virtual void ApplyPagination(int? page, int pageSize)
        {
            ApplyPagination(page ?? 1, pageSize);
        }

        public virtual void ApplyPagination(int page, int pageSize)
        {
            if (Items != null )
            {
                if (page <= 0)
                {
                    page = 1;
                }
                Items = Items.AsPagination(page, pageSize);
            }
        }
    }
}